package scantask.client.presentationLayer;

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.view.orbit.OrbitView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import scantask.client.domainLayer.ControllerInterface;
import scantask.containers.Coordinate;
import scantask.containers.exceptions.UnInitializeObjectException;
import scantask.server.domainLayer.Exceptions.LoginException;

public class CoordinatesPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// The world window of NASA.
	final private WorldWindow _wwd;
	
	// The client controller.
	private ControllerInterface _controller;
	
	public CoordinatesPanel(WorldWindow wwd, ControllerInterface controller) {
		
		super();
		this.setLayout(null);
		this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
	
		this._wwd = wwd;
		this._controller = controller;
		JLabel coor = new JLabel();
		coor.setText("Coordinates:");
		coor.setBounds(10, 10, 100, 25);
        this.add(coor);
		
		JLabel lat = new JLabel();
		lat.setText("Lat:");
		lat.setBounds(10, 45, 40, 25);
		this.add(lat);
		
		final JTextArea latText = new JTextArea();
		latText.setBounds(60, 45, 140, 25);
        this.add(latText);
        
        JLabel lon = new JLabel();
		lon.setText("Lon:");
		lon.setBounds(10, 80, 40, 25);
		this.add(lon);
		
		final JTextArea lonText = new JTextArea();
        this.add(lonText);
        lonText.setBounds(60, 80, 140, 25);
        
        JButton go = new JButton("GO");
        go.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					double doubleLat = Double.parseDouble(latText.getText());
					double doubleLon = Double.parseDouble(lonText.getText());
					OrbitView view = (OrbitView)CoordinatesPanel.this._wwd.getView();
					// Fly to the new position.
					view.goTo(new Position(
							LatLon.fromDegrees(doubleLat, doubleLon),
							view.getZoom()),view.getZoom());
					CoordinatesPanel.this.update();
							
				}
				catch (Exception exc) {
					JOptionPane.showMessageDialog(
							null, "Coordinates must be numbers",
							"Error  ", JOptionPane.ERROR_MESSAGE);
				}
			}
        	
        });
        go.setBounds(10, 115, 190, 25);
        this.add(go);
        
        JButton back = new JButton("Back to default coordinates");
        back.addActionListener(new ActionListener() {
        	
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					Coordinate c = CoordinatesPanel.this._controller.
							getDefaultCoordinate();
					CoordinatesPanel.this._wwd.getView().goTo(new Position(
							LatLon.fromDegrees(c.getY(), c.getX()),
							FunctionalityPanel.DEFAULT_ZOOM),
							FunctionalityPanel.DEFAULT_ZOOM);
				} catch (LoginException e1) {

					e1.printStackTrace();
				} catch (UnInitializeObjectException e1) {

					e1.printStackTrace();
				}
				
			}
        	
        });
        back.setBounds(10, 150, 190, 25);
        this.add(back);
        
        JButton curr = new JButton("Show current position");
        curr.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				OrbitView view = (OrbitView)CoordinatesPanel.this._wwd.getView();
				// Get the current position from the globe.
				Position pos = view.getCenterPosition();
				double x = pos.getLatitude().degrees;
				double y = pos.getLongitude().degrees;
				latText.setText(x + "");
				lonText.setText(y + "");
			}
        	
        });
        curr.setBounds(10, 185, 190, 25);
        this.add(curr);
	}
	
	// Update Globe.
    private void update()
    {
        this._wwd.redraw();
    }

}
